<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,
	    user-scalable=no,initial-scale=1.0,maximum=1.0,minimum=1.0">
		<title>授权</title>
		<!-- 引入jquery-->
		<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
		<!-- 引入 1.3.2的js-sdk文件 -->
		<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
		<link rel="stylesheet" href="load.css" media="all">
		<script src="load-min.js" charset="utf-8"></script>

	</head>
	<style>
		.test_mask{
			    position: absolute;
			    top: 50%;
			    left: 50%;
			    transform: translate(-50%, -50%);
		}
		
	</style>
	<body>
		<div id="test_mask_1" class="test_mask">
		</div>
	</body>
	<script>
		$.mask_fullscreen();
		function getCode() {
			var code = "";
			var local = window.location.href; // 获取页面url
			var appid = "wxcd61c92313ae44af";
			code = getUrlCode().code; // 截取code
			if (code == null || code === "") {
				// 如果没有code，则去请求
				window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
	                local
	            )}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`;
				console.log(code)
				// scope=snsapi_base      静默授权,自动跳转到回调页的  特点：用户无感知；
				// scope=snsapi_userinfo  非静默授权，第一次有弹框
			} else {
				// console.log(code)
				// 你自己的业务逻辑
				// 在这里跳回自己的小程序，并且把获取到的code传递给小程序
				//参考文档：https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
				$.mask_close_all();
				wx.miniProgram.redirectTo({
					url: '/my_pages/resCode/resCode?code=' + code
				})
			}
		}
		getCode()
		// 截取url中的code方法
		function getUrlCode() {
			var url = location.search;
			var theRequest = new Object();
			if (url.indexOf("?") != -1) {
				var str = url.substr(1);
				var strs = str.split("&");
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
				}
			}
			return theRequest;
		}
	</script>
</html>
